package service

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/repository"
	"boy-go/modules/system/vo"
	"boy-go/pkg/request"
	"github.com/jinzhu/copier"
)

func NewSysPostService() SysPostService {
	return &sysPostService{
		SysPostRepo: repository.NewSysPostRepository(),
	}
}

type sysPostService struct {
	SysPostRepo *repository.SysPostRepository
}

// GetById 根据id获取配置信息
func (s *sysPostService) GetById(postId int64) (*vo.SysPostModel, error) {
	m, err := s.SysPostRepo.GetById(postId)
	if err != nil {
		return nil, err
	}
	rsp := new(vo.SysPostModel)
	if err := copier.Copy(rsp, m); err != nil {
		return nil, err
	}
	return rsp, nil
}
func (s *sysPostService) SelectPostListByUserId(userId int64) []*int64 {
	postIds, err := s.SysPostRepo.SelectPostListByUserId(userId)
	if err != nil {
		return nil
	}
	return postIds
}

// List 获取岗位信息表列表
func (s *sysPostService) List(req *vo.QuerySysPostReq) (*vo.ListSysPostRsp, error) {

	var pageReq = &request.PageRequest{
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
	}

	list, total, err := s.SysPostRepo.List(req, pageReq)

	if err != nil {
		return nil, err
	}

	itemList := make([]*vo.SysPostModel, 0)
	for _, v := range list {
		item := new(vo.SysPostModel)
		if err := copier.Copy(item, &v); err != nil {
			continue
		}
		itemList = append(itemList, item)
	}
	return &vo.ListSysPostRsp{
		List:  itemList,
		Total: total,
	}, nil
}

func (s *sysPostService) SelectPostAll() []*vo.SysPostModel {
	list, _, err := s.SysPostRepo.List(nil, nil)
	if err != nil {
		return nil
	}
	itemList := make([]*vo.SysPostModel, 0)
	for _, v := range list {
		item := new(vo.SysPostModel)
		if err := copier.Copy(item, &v); err != nil {
			continue
		}
		itemList = append(itemList, item)
	}
	return itemList
}

// Add 添加岗位信息表
func (s *sysPostService) Add(req *vo.AddSysPostReq) (*vo.AddSysPostRsp, error) {
	m := new(model.SysPost)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysPostRepo.Save(m); err != nil {
		return nil, err
	}
	return &vo.AddSysPostRsp{
		PostId: m.PostId,
	}, nil
}

// Edit 修改岗位信息表
func (s *sysPostService) Edit(req *vo.EditSysPostReq) (*vo.EditSysPostRsp, error) {
	m := new(model.SysPost)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysPostRepo.Update(m); err != nil {
		return nil, err
	}
	return &vo.EditSysPostRsp{
		PostId: m.PostId,
	}, nil
}

// Del 删除岗位信息表
func (s *sysPostService) Del(postId int64) error {
	return s.SysPostRepo.Delete(postId)
}

// BatchDel 批量删除岗位信息表
func (s *sysPostService) BatchDel(ids []string) error {
	return s.SysPostRepo.BatchDelete(ids)
}
